﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSM.Lib.Protocol;
using CSM.DAL;
using System.Data;
using CSM.Lib;

namespace CSM.BLL.UserManager
{
	public class UserManager
	{
		static List<UserInfo> Users = new List<UserInfo>();
		public UserInfo this[long id]
		{
			get { return Users.Find(delegate(UserInfo user) { return id == user.UserID; }); }
		}

		public UserInfo this[string eMail]
		{
			get { return Users.Find(delegate(UserInfo user) { return eMail == user.Email; }); }
		}

		public static UserInfo Login(UserLoginReq uiReq)
		{
			DataTable dbui = DBUserInfo.QueryUserInfo(uiReq.LoginKey);
			UserInfo ui = null;

			if (dbui != null && dbui.Rows.Count == 1)
			{
				DataRow dr = dbui.Rows[0];
				ui = new UserInfo();
				ui.ClientVer = uiReq.ClientVer;
				if (uiReq.Password == (string)dr["password"])
				{
					ui.UserID = (long)dr["user_id"];
					ui.TrueName = (string)dr["true_name"];
					ui.NickName = (string)dr["nick_name"];
					ui.Birthday = (DateTime)dr["birthday"];
					ui.Gender = (Gender)((bool)dr["gender"] ? 0 : 1);
					ui.Email = (string)dr["email"];
					ui.UsrType = (Int32)dr["user_type"];
					ui.UsrState = (Int32)dr["user_state"];

					Users.Add(ui);
				}
			}

			return ui;
		}
	}
}
